package com.hellohu.mbg.plugin;

import org.mybatis.generator.api.IntrospectedTable;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.function.BiFunction;

/**
 * 简单的插件工具类
 *
 * @author hellhu
 */
public class PluginUtil {

    private static final String author = "mbg";

    /**
     * 返回注释
     *
     * @param introspectedTable mbg的table
     * @return 注释
     */
    public static List<String> getComment(IntrospectedTable introspectedTable, BiFunction<String, String, String> classCommentFunction) {

        List<String> result = new ArrayList<>();
        // 获取表注释
        String remark = introspectedTable.getRemarks();
        String tableName = introspectedTable.getFullyQualifiedTable().getIntrospectedTableName();
        String comment = classCommentFunction.apply(tableName, remark);
        result.add("/**");
        result.add(" * " + comment);
        result.add(" *");
        result.add(" * @author " + author);
        result.add(" */");
        return result;
    }

    /**
     * 获取boolean类型的属性
     *
     * @param properties   属性
     * @param key          key
     * @param defaultValue 默认值
     * @return 存在key返回对应的value，否则返回默认值
     */
    public static boolean getBooleanProperty(Properties properties, String key, boolean defaultValue) {
        String strEnable = properties.getProperty(key, defaultValue ? "true" : "false");
        return "true".equals(strEnable);
    }
}
